//
//  LFTMCTeamMembersView.m
//  lofter-mvpcourse-ios
//
//  Created by Thomas Quan on 2017/5/4.
//  Copyright © 2017年 undefined-zola. All rights reserved.
//

#import "LFTMCTeamMemberCell.h"
#import "UIView+NIM.h"
#import "NIMUsrInfoData.h"
#import "NIMAvatarImageView.h"
#import "NIMKitUtil.h"
#import "NIMKit.h"
#import "NIMTeamMemberListViewController.h"
#import "LFTMCTeamMemberListViewController.h"
#import "LFTMCTeamMemberCardViewController.h"
#import "LFTMCUtil.h"

@interface LFTMCTeamMemberView : UIView

@property (nonatomic, weak) id<LFTMCTeamMemberCellActionDelegate> delegate;

@property(nonatomic,strong) NIMAvatarImageView *imageView;

@property(nonatomic,strong) UILabel *titleLabel;

@property(nonatomic,strong) NIMKitInfo *userInfo;

@property(nonatomic, strong) NIMTeamMember *member;

@end

#define RegularTeamMemberViewHeight 53
#define RegularTeamMemberViewWidth  45
@implementation LFTMCTeamMemberView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.font = [UIFont systemFontOfSize:12.f];
        [self addSubview:_titleLabel];
        _imageView  = [[NIMAvatarImageView alloc] initWithFrame:CGRectMake(0, 0, 45, 45)];
        [_imageView addTarget:self action:@selector(didClickSelf) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_imageView];
    }
    return self;
}

- (void)setUserInfo:(NIMKitInfo *)userInfo{
    _userInfo = userInfo;
    NSURL *avatarURL;
    if (_userInfo.avatarUrlString.length) {
        avatarURL = [NSURL URLWithString:_userInfo.avatarUrlString];
    }
    [_imageView nim_setImageWithURL:avatarURL placeholderImage:_userInfo.avatarImage];
}


- (CGSize)sizeThatFits:(CGSize)size{
    return CGSizeMake(RegularTeamMemberViewWidth, RegularTeamMemberViewHeight);
}

- (void)layoutSubviews{
    [super layoutSubviews];
    [self.titleLabel sizeToFit];
    self.titleLabel.nim_width = _titleLabel.nim_width > self.nim_width ? self.nim_width : _titleLabel.nim_width;
    self.imageView.nim_centerX = self.nim_width * .5f;
    self.titleLabel.nim_centerX = self.nim_width * .5f;
    self.titleLabel.nim_top = self.imageView.nim_bottom + 10;
}

- (void)didClickSelf {
    
    if ([_delegate respondsToSelector:@selector(memberCardAction:)]) {
        LFTMCTeamMemberCardViewController *vc = [[LFTMCTeamMemberCardViewController alloc] initWithUserId:_member.userId team:_member.teamId];
        [_delegate memberCardAction:vc];
    }
}

@end

@interface LFTMCTeamMemberContainerView : UIView

@property (nonatomic, weak) id<LFTMCTeamMemberCellActionDelegate> delegate;

@property (nonatomic, strong) NSMutableArray *icons;

@property (nonatomic, strong) NIMTeam *team;

@property (nonatomic, copy)   NSArray *teamMembers;

@property (nonatomic, strong) LFTMCTeamMemberView *teamMemberView;

- (void)draw;

@end

@implementation LFTMCTeamMemberContainerView

- (instancetype)initWithTeam:(NIMTeam *)team
                    members:(NSArray *)members
{
    self = [super init];
    if (self) {
        _icons = [[NSMutableArray alloc] init];
        _team = team;
        _teamMembers = members;
    }
    return self;
}

- (void)draw
{

    NSInteger maxIconCount = 5;
    NSInteger iconCount = _teamMembers.count > maxIconCount ? maxIconCount : _teamMembers.count;
    NIMSession *session = [NIMSession session:_team.teamId type:NIMSessionTypeTeam];
    for (UIView *view in _icons) {
        [view removeFromSuperview];
    }
    for (NSInteger i = 0; i < iconCount; i++) {
        LFTMCTeamMemberView *view = [self fetchMemeberView:i];
        NIMTeamMember *member       = _teamMembers[i];
        NIMKitInfo *info            = [[NIMKit sharedKit] infoByUser:member.userId option:nil];
        view.member                 = member;
        view.userInfo               = info;
        view.titleLabel.text        = [NIMKitUtil showNick:member.userId inSession:session];
        [self addSubview:view];
        [view setNeedsLayout];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGSize size = [UIScreen mainScreen].bounds.size;
    CGFloat screenWidth = size.width;
    CGFloat left = screenWidth / 25;
    CGFloat spacing = screenWidth / 12.5;
    CGFloat top = 20.f;

    for (LFTMCTeamMemberView *view in _icons) {
        view.nim_top = top;
        view.nim_left = left;
        left += view.nim_width;
        left += spacing;
    }
}

- (LFTMCTeamMemberView *)fetchMemeberView:(NSInteger)index{
    if (_icons.count <= index) {
        for (int i = 0; i < index - _icons.count + 1; i++) {
            LFTMCTeamMemberView *view = [[LFTMCTeamMemberView alloc] initWithFrame:CGRectZero];
//            view.userInteractionEnabled = NO;
            view.delegate = _delegate;
            [view sizeToFit];
            [_icons addObject:view];
        }
    }
    return _icons[index];
}


@end

@interface LFTMCMoreTeamMemberView : UIView

@property (nonatomic, strong) UIButton *moreBtn;

@property (nonatomic, strong) UIImageView *imageView;

@property (nonatomic, strong) NIMTeam *team;

@property (nonatomic, copy)   NSArray *teamMembers;

@property (nonatomic, weak) id<LFTMCTeamMemberCellActionDelegate> delegate;

@end

@implementation LFTMCMoreTeamMemberView

- (instancetype)initWithTeam:(NIMTeam *)team
                          members:(NSArray *)members
{
    self = [super init];
    if (self) {
        _team = team;
        _teamMembers = members;
        _moreBtn = [[UIButton alloc] init];
        [_moreBtn setTitle:@"更多群成员" forState:UIControlStateNormal];
        _moreBtn.titleLabel.font = [UIFont systemFontOfSize:14.f];
        [_moreBtn setTitleColor:NIMKit_UIColorFromRGB(0x999999) forState:UIControlStateNormal];
        [_moreBtn.titleLabel sizeToFit];
        [_moreBtn addTarget:self action:@selector(didClickMoreBtn) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_moreBtn];
        
        _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"LFTMC_More"]];
        [self addSubview:_imageView];

    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = 49.f;
    _moreBtn.nim_width = 80;
    _moreBtn.nim_height = 20;
    _moreBtn.nim_centerX = width / 2;
    _moreBtn.nim_centerY = height / 2;
    
    _imageView.nim_width = 7;
    _imageView.nim_height = 12;
    _imageView.nim_left = _moreBtn.nim_right + 5;
    _imageView.nim_centerY = height / 2;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self didClickMoreBtn];
}

- (void)didClickMoreBtn {
    if ([_delegate respondsToSelector:@selector(memberCardAction:)]) {
        [[LFTMCUtil sharedInstance].dataSource lftmc_trackEventID:@"GroupMembersClick"
                                                         category:nil
                                                            label:nil
                                                       attributes:nil];
        LFTMCTeamMemberListViewController *vc = [[LFTMCTeamMemberListViewController alloc] initTeam:_team members:_teamMembers];
        [_delegate memberCardAction:vc];
    }
}

@end

@interface LFTMCTeamMemberCell()

@property(nonatomic, strong) LFTMCTeamMemberContainerView *containerView;

@property (nonatomic, strong) UIView *lineView;

@property(nonatomic, strong) LFTMCMoreTeamMemberView *moreView;

@end

@implementation LFTMCTeamMemberCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifie {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifie];
    if (self) {
        
    }
    return self;
}

- (void)refreshWithTeam:(NIMTeam *)team
                members:(NSArray *)members
                  width:(CGFloat)width {

    _containerView = [[LFTMCTeamMemberContainerView alloc] initWithTeam:team members:members];
    _containerView.delegate = _delegate;
    [_containerView draw];
    [self addSubview:_containerView];
    
    _lineView = [[UIView alloc] init];
    _lineView.backgroundColor = NIMKit_UIColorFromRGB(0xEEEEEE);
    [self addSubview:_lineView];
    
    _moreView = [[LFTMCMoreTeamMemberView alloc] initWithTeam:team members:members];
    _moreView.delegate = _delegate;
    [self addSubview:_moreView];

}

- (void)layoutSubviews{
    [super layoutSubviews];
    CGSize size = [UIScreen mainScreen].bounds.size;
    CGFloat screenWidth = size.width;
    
    _containerView.nim_left = 0.f;
    _containerView.nim_top = 0.f;
    _containerView.nim_height = 100.f;
    _containerView.nim_width = screenWidth;
    
    _lineView.nim_height = 1.f;
    _lineView.nim_top = _containerView.nim_bottom;
    _lineView.nim_width = screenWidth;
    _lineView.nim_left = 0.f;
    
    _moreView.nim_width = screenWidth;
    _moreView.nim_top = _containerView.nim_bottom;
    _moreView.nim_left = 0.f;
    _moreView.nim_height = 49.f;
    
}



@end
